package com.viewpagertext.DBHelper.Dao;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.viewpagertext.json.StarredSong;

import java.util.List;

@Dao
public interface StarredSongDao {

    // 插入一首歌曲
    @Insert
    long insertSong(StarredSong song);

    // 读取所有收藏的歌曲信息
    @Query("SELECT * FROM starred_songs")
    List<StarredSong> getAllStarredSongs();

    // 根据名字查询对应的歌曲
    @Query("SELECT * FROM starred_songs WHERE name = :name")
    StarredSong getStarredSongByName(String name);

    // 查询数据库中的第一首歌曲
    @Query("SELECT * FROM starred_songs ORDER BY id DESC LIMIT 1;")
    StarredSong getFirstStarredSong();

    // 根据歌曲名和歌手名查询对应的歌曲
    @Query("SELECT * FROM starred_songs WHERE name = :name AND singer = :singer")
    StarredSong getStarredSongByNameAndSinger(String name, String singer);

    // 更新特定歌曲的URL
    @Query("UPDATE starred_songs SET url = :url WHERE name = :name AND singer = :singer")
    void updateByNameAndSinger(String name, String singer, String url);

    // 根据ID删除一首歌曲
    @Query("DELETE FROM starred_songs WHERE id = :songId")
    void deleteStarredSongById(int songId);

    // 根据歌曲名和歌手名删除对应的歌曲
    @Query("DELETE FROM starred_songs WHERE name = :name AND singer = :singer")
    void deleteStarredSongByNameAndSinger(String name, String singer);
    @Query("UPDATE  starred_songs SET id = id -1 WHERE id > :from;")
    void updateWhereId(int from);
    // 更新一首歌的信息
    @Update
    void updateStarredSong(StarredSong song);

    // 获取所有已下载的歌曲
    @Query("SELECT * FROM starred_songs WHERE isDownloaded = 1")
    List<StarredSong> getAllDownloadedSongs();

    // 这两个查询之前定义
    @Query("SELECT * FROM starred_songs WHERE id > COALESCE((SELECT id FROM starred_songs WHERE name = :name AND singer = :singer LIMIT 1), 0) ORDER BY id ASC LIMIT 1")
    StarredSong getNextSong(String name, String singer);

    @Query("SELECT * FROM starred_songs ORDER BY id ASC LIMIT 1")
    StarredSong getFirstSong();

    // 调用这个方法来获取下一首或者第一首歌曲
    default StarredSong getNextOrFirstStarredSong(String name, String singer) {
        StarredSong nextSong = getNextSong(name, singer);
        if (nextSong == null) {
            System.out.println("下一首歌曲不存在");
            nextSong = getFirstSong();
        }
        return nextSong;
    }

    // 这两个查询之前定义
    @Query("SELECT * FROM starred_songs WHERE id < COALESCE((SELECT id FROM starred_songs WHERE name = :name AND singer = :singer LIMIT 1), (SELECT MAX(id) FROM starred_songs) + 1) ORDER BY id DESC LIMIT 1")
    StarredSong getPreviousSong(String name, String singer);

    @Query("SELECT * FROM starred_songs ORDER BY id DESC LIMIT 1")
    StarredSong getLastSong();

    // 调用这个方法来获取上一首或者最后一首歌曲
    default StarredSong getPreviousOrLastStarredSong(String name, String singer) {
        StarredSong previousSong = getPreviousSong(name, singer);
        if (previousSong == null) {
            System.out.println("上一首歌曲不存在");
            previousSong = getLastSong();
        }
        return previousSong;
    }
    @Query("SELECT MAX(id) FROM starred_songs;")
    int getMaxId();
}
